package com.mb.ext.core.dao.impl.group;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.mb.ext.core.dao.AbstractBaseDAO;
import com.mb.ext.core.dao.group.GroupBuyDAO;
import com.mb.ext.core.entity.group.GroupBuyEntity;
import com.mb.ext.core.service.spec.GroupBuySearchDTO;
import com.mb.framework.exception.DAOException;
import com.mb.framework.util.log.LogHelper;

@Repository("groupBuyDAO")
@Qualifier("groupBuyDAO")
public class GroupBuyDAOImpl extends AbstractBaseDAO<GroupBuyEntity> implements GroupBuyDAO {
	private final LogHelper logger = LogHelper.getInstance(this.getClass().getName());

	/**
	 * Initializing Entity.
	 */
	public GroupBuyDAOImpl() {
		super();
		this.setEntityClass(GroupBuyEntity.class);
	}

	@Override
	public void addGroupBuy(GroupBuyEntity groupBuyEntity) throws DAOException {

		this.save(groupBuyEntity);

	}

	@Override
	public void updateGroupBuy(GroupBuyEntity groupBuyEntity) throws DAOException {

		this.update(groupBuyEntity);

	}

	@Override
	public void deleteGroupBuy(GroupBuyEntity groupBuyEntity) throws DAOException {

		this.delete(groupBuyEntity);

	}

	@Override
	public GroupBuyEntity getGroupBuyByUuid(String uuid) throws DAOException {
		GroupBuyEntity groupBuyEntity = null;
		try {
			groupBuyEntity = (GroupBuyEntity) em.createQuery(
					"select b from GroupBuyEntity b where  b.groupBuyUuid = :UUID and b.isDeleted=:isDeleted",
					GroupBuyEntity.class).setParameter("UUID", uuid).setParameter("isDeleted", Boolean.FALSE)
					.getSingleResult();
		} catch (NoResultException e) {
			logger.info("No record found for product : " + uuid);
		}
		return groupBuyEntity;
	}

	@Override
	public List<GroupBuyEntity> getActiveGroupBuys() throws DAOException {
		List<GroupBuyEntity> groupBuyEntityList = new ArrayList<GroupBuyEntity>();
		try {
			groupBuyEntityList = em.createQuery(
					"select b from GroupBuyEntity b where b.status = :STATUS and b.isDeleted=:isDeleted order by b.createDate desc",
					GroupBuyEntity.class)
					.setParameter("STATUS", "1")
					.setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return groupBuyEntityList;
	}

	@Override
	public List<GroupBuyEntity> getAllGroupBuys() throws DAOException {
		List<GroupBuyEntity> groupBuyEntityList = new ArrayList<GroupBuyEntity>();
		try {
			groupBuyEntityList = em.createQuery("select b from GroupBuyEntity b where  b.isDeleted=:isDeleted order by b.createDate desc",GroupBuyEntity.class).setParameter("isDeleted", Boolean.FALSE).getResultList();
		} catch (NoResultException e) {
			logger.info("No record found for sec kill");
		}
		return groupBuyEntityList;
	}

	@Override
	public List<GroupBuyEntity> searchGroupBuy(GroupBuySearchDTO groupBuySearchDTO) throws DAOException {

		List<GroupBuyEntity> orderEntityList = new ArrayList<GroupBuyEntity>();
		String query = "select b.groupBuyEntity from GroupBuyUserEntity b where b.isDeleted=:isDeleted";
		String[] keyArray = groupBuySearchDTO.getKeyArray();
		for (int i = 0; i < keyArray.length; i++) {
			String key = keyArray[i];
			 if (GroupBuySearchDTO.KEY_USER.equals(key)) {
				query = query + " and b.userEntity.userUuid = :USERUUID";
			} else if (GroupBuySearchDTO.KEY_GROUP_BUY_STATUS.equals(key)) {
				query = query + " and b.groupBuyEntity.status = :STATUS";
			}
		}
		query = query + " order by b.groupBuyEntity.startTime desc";	//按时间降序排列
		try {
			TypedQuery typedQuery = em.createQuery(query, GroupBuyEntity.class);
			for (int i = 0; i < keyArray.length; i++) {
				String key = keyArray[i];
				if (GroupBuySearchDTO.KEY_GROUP_BUY_STATUS.equals(key)) {
					typedQuery.setParameter("STATUS",
							groupBuySearchDTO.getGroupBuyStatus());
				} else if (GroupBuySearchDTO.KEY_USER.equals(key)) {
					typedQuery.setParameter("USERUUID",
							groupBuySearchDTO.getUserUuid());
				}
			}
			orderEntityList = typedQuery
					.setParameter("isDeleted", Boolean.FALSE)
					.setFirstResult(groupBuySearchDTO.getStartIndex()).setMaxResults(groupBuySearchDTO.getPageSize())
					.getResultList();
		} catch (NoResultException e) {
			logger.info("No record found");
		}
		return orderEntityList;
	
	}
	
	@Override
	public int searchGroupBuyTotal(GroupBuySearchDTO groupBuySearchDTO) throws DAOException {

		Long total = null;
		String query = "select count(b.groupBuyEntity) from GroupBuyUserEntity b where b.isDeleted=:isDeleted";
		String[] keyArray = groupBuySearchDTO.getKeyArray();
		for (int i = 0; i < keyArray.length; i++) {
			String key = keyArray[i];
			 if (GroupBuySearchDTO.KEY_USER.equals(key)) {
				query = query + " and b.userEntity.userUuid = :USERUUID";
			} else if (GroupBuySearchDTO.KEY_GROUP_BUY_STATUS.equals(key)) {
				query = query + " and b.groupBuyEntity.status = :STATUS";
			}
		}
		query = query + " order by b.groupBuyEntity.startTime desc";	//按时间降序排列
		try {
			TypedQuery typedQuery = em.createQuery(query, Long.class);
			for (int i = 0; i < keyArray.length; i++) {
				String key = keyArray[i];
				if (GroupBuySearchDTO.KEY_GROUP_BUY_STATUS.equals(key)) {
					typedQuery.setParameter("STATUS",
							groupBuySearchDTO.getGroupBuyStatus());
				} else if (GroupBuySearchDTO.KEY_USER.equals(key)) {
					typedQuery.setParameter("USERUUID",
							groupBuySearchDTO.getUserUuid());
				}
			}
			total = (Long) typedQuery
					.setParameter("isDeleted", Boolean.FALSE)
					.setFirstResult(groupBuySearchDTO.getStartIndex()).setMaxResults(groupBuySearchDTO.getPageSize())
					.getSingleResult();
		} catch (NoResultException e) {
			logger.info("No record found");
		}
		return total == null ? 0 : total.intValue();
	
	}
}
